import { Request, toast, formatTimeQuantity } from '../../utils/util.js'
let request = new Request()
const app = getApp()
Component({
  data: {
    orderList : []
  },
  methods : {
    getOrderList() {
      request.get('order/charging', res => {
        if (res.success) {
          let time = res.data.current_time
          let orderList = res.data.list
          if (orderList.length > 0) {
            orderList.forEach(order => {
              try{
                let offsetTime = Math.floor((time - order.start_time) / 60) //当前距离开始充电多长时间
                let chargeType = order.charge_type
                let quantity = order.charge_quantity
                if (offsetTime <= 1) { //一分钟内
                  order.surplus = chargeType == 1 ? formatTimeQuantity(quantity) : quantity + '度'
                } else if (offsetTime <= 2) { //两分钟以内
                  order.surplus = chargeType == 1 ? formatTimeQuantity(quantity - offsetTime) : '&cong;' + quantity + '度'
                } else {
                  order.surplus = chargeType == 1 ? formatTimeQuantity(order.data.surplus_time) : order.data.surplus_energy + '度'
                }
              }catch(e){}
            })
          }else{
            orderList = null
          }
          

          this.setData({ orderList: orderList })
        }
      })
    },
    
    stopCharge(e) {
      let index = parseInt(e.currentTarget.dataset.index)
      let order = this.data.orderList[index]
      let orderId = order.order_id
      let orderNo = order.order_no
      wx.showModal({
        title: '停止充电',
        content: '确定停止充电吗',
        success: res => {
          if (res.confirm) {
            wx.showLoading({
              title: ''
            })
            request.post('stopCharge', res => {
              if (res.success) {
                let t = 0
                let inte = setInterval(() => {
                  request.get('chargestatus', res => {
                    if (res.success) {
                      if (res.data.status == 2) { //停止成功
                        clearInterval(inte)
                        wx.navigateTo({
                          url: '/pages/order/detail/index?orderNo=' + orderNo,
                          success: (res) => {
                            wx.hideLoading()
                            let orderList = this.data.orderList
                            orderList.splice(index, 1)
                            this.setData({ orderList: orderList })
                          }
                        })
                      } else {
                        if (t >= 60) {
                          clearInterval(inte)
                          wx.hideLoading()
                          toast('停止失败')
                          return
                        }
                        t += 2
                      }
                    } else {
                      clearInterval(inte)
                      toast(res.msg)
                    }
                  }, { id: orderId })
                }, 2000)
              } else {
                toast(res.msg)
              }
            }, { id: orderId })
          }
        }
      })

    },
  },

  pageLifetimes : {
    show : function (options) {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selectedIndex: 1
        })
      }

      this.getOrderList()
    }
  }
  
})